﻿
using UnityEngine;
using System.Collections;
using System;
using YanhuaMMO;
using Spine;
using Spine.Unity;
public class SkillInfo
{
    public int[] leftModelIds;
    public int[] leftFighterIds;
    public int[] rightModelIds;
    public int[] rightFighterIds;
    public int[] skillIds;
    public int[] skillIdDelays;
    public int[] targetIndexs;
    public string[] skillDatas;
    public string[] skillShows;
    public int fighterIndex;

    public int getLeftModelIdsCount()
    {
        return leftModelIds.Length;
    }
    public int getLeftFighterIdsCount()
    {
        return leftFighterIds.Length;
    }

    public int getRightModelIdsCount()
    {
        return rightModelIds.Length;
    }
    public int getRightFighterIdsCount()
    {
        return rightFighterIds.Length;
    }

    public int getSkillIdsCount()
    {
        return skillIds.Length;
    }

    public int getSkillIdDelaysCount()
    {
        return skillIdDelays.Length;
    }

    public int getTargetIndexsCount()
    {
        return targetIndexs.Length;
    }

    public int getSkillDataCount()
    {
        return skillDatas.Length;
    }

    public int getSkillShowCount()
    {
        return skillShows.Length;
    }
}

public class SkillPlayMgr : MonoBehaviour {
    /*
        public GameObject leftRolePrefab;
        public GameObject rightMonsterPrefab;
    */
    private GameObject playerObj;
    private GameObject monsterObj;

    private Coroutine playco;
    private GameObject attackcircle;
    private bool hasInit = false;
    private Coroutine playReportCo;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
		
	}

    public void Init()
    {
        Debug.Log("SkillPlayMgr:Init");
        Util.CallMethod("CSharpModel", "initSkillRoleMgr", new object[] { });
    }

    public void addNewRole(string modelId)
    {
        if (!hasInit)
        {
            Init();
            hasInit = true;
            LuaHelper.GetPanelManager().BattleRound.gameObject.SetActive(true);
        }

        Vector3 pos = new Vector3(304, 300, 294);
        //Vector3 pos = new Vector3(310, 300, 294);
        object[] obj = Util.CallMethod("CSharpModel", "addLeftPlayer", new object[] { modelId, pos});
        if (playerObj)
            DestroyImmediate(playerObj);
        playerObj = obj[0] as GameObject;
    }

    public void updateAttackRange(float radius)
    {
        if(attackcircle != null)
        {
            attackcircle.transform.localScale = playerObj.transform.localScale * radius;
        }
    }
    public void addRightNewRole(string modelId)
    {
        if (!hasInit)
        {
            Init();
            hasInit = true;
            LuaHelper.GetPanelManager().BattleRound.gameObject.SetActive(true);
        }

        //Vector3 pos = new Vector3(304, 300, 294);
        Vector3 pos = new Vector3(310, 300, 294);
        object[] obj = Util.CallMethod("CSharpModel", "addRightPlayer", new object[] { modelId, pos });
        if (monsterObj)
            DestroyImmediate(monsterObj);
        monsterObj = obj[0] as GameObject;
    }

    public void playReport(string filepath, string savePath) {
        if (!hasInit)
        {
            Init();
            hasInit = true;
        }
        Debug.Log("playReport");
        if (playReportCo != null)
            StopCoroutine(playReportCo);

        playReportCo = StartCoroutine(PlayReportCoroutine(filepath, savePath, 0));
    }

    IEnumerator PlayReportCoroutine(string filepath, string savePath ,float delaytime)
    {
        yield return new WaitForSeconds(delaytime);
        Util.CallMethod("CSharpModel", "playReport", new object[] { filepath, savePath });
    }

    public string[] getAllAnimation()
    {
        string[] clipname = new string[1];
        if (playerObj != null)
        {
            SkeletonAnimation anim = playerObj.transform.Find("Animation").GetComponent<SkeletonAnimation>();
            clipname = new string[anim.skeleton.Data.Animations.Items.Length + 1];
            clipname[0] = "null";
            for (int i = 0; i < anim.skeleton.Data.Animations.Items.Length; i++)
                clipname[i + 1] = anim.skeleton.Data.Animations.Items[i].Name;

        }
        else
            return null;
        return clipname;
    }

    public void playAnimation(string aniName)
    {
        if (playerObj)
        {
            SkeletonAnimation anim = playerObj.transform.Find("Animation").GetComponent<SkeletonAnimation>();
            anim.skeleton.SetToSetupPose();
            anim.state.SetAnimation(0, "idle01", false);

            anim.state.SetAnimation(0, aniName, false);
            StartCoroutine(WaitPlayCompleteAction());
        }
    }

    /// <summary>
    /// 等待伤害动作播放完成
    /// </summary>
    /// <returns></returns>
    public IEnumerator WaitPlayCompleteAction()
    {
        SkeletonAnimation anim = playerObj.transform.Find("Animation").GetComponent<SkeletonAnimation>();


        var trackEntry = anim.state.GetCurrent(0);
        
        yield return new WaitUntil(() => { return trackEntry.IsComplete; });

        anim.state.SetAnimation(0, "idle01", true);

    }

    public void playSkill(string skillid,string skilldata,string skillshow,bool isloop,float delaytime)
    {
        if (playco != null)
            StopCoroutine(playco);

        Util.CallMethod("CSharpModel", "playSkill", new object[] { skillid, skilldata, skillshow});
        if(isloop)
        {
            playco = StartCoroutine(coPlaySkill(skillid, skilldata, skillshow, delaytime));
        }
    }

    public void playSkills(SkillInfo info)
    {
        if (!hasInit)
        {
            Init();
            hasInit = true;
            LuaHelper.GetPanelManager().BattleRound.gameObject.SetActive(true);
        }

        Util.CallMethod("CSharpModel", "playSkills", new object[] { info });
    }

    IEnumerator coPlaySkill(string skillid, string skilldata, string skillshow, float delaytime)
    {
        while (true)
        {
            yield return new WaitForSeconds(delaytime);

            Util.CallMethod("CSharpModel", "playSkill", new object[] { skillid, skilldata, skillshow, delaytime });
        }
    }

    public void stopPlaySkill()
    {
        Util.CallMethod("CSharpModel", "stopplaySkill", new object[] { });
    }

    public bool IsRoleInit()
    {
        return playerObj && monsterObj;
    }
}
